﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Admin_quan_ly_nha_hang_slide : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetAllResSlide();
            GetAllRes();
        }
    }
    protected void grvResSlide_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvResSlide.EditIndex = -1;
        GetAllResSlide();
    }
    protected void grvResSlide_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string i = Convert.ToString(grvResSlide.DataKeys[e.RowIndex].Values);
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_DeleteResSlide";
            Dal.ParameterCollection = new string[1] { "@SlideID" };
            Dal.ValueCollection = new string[1] { i };
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAllResSlide();
        }
        catch { }
    }
    protected void grvResSlide_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvResSlide.EditIndex = e.NewEditIndex;
        GetAllResSlide();
    }
    protected void grvResSlide_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string i = Convert.ToString(grvResSlide.DataKeys[e.RowIndex].Value);
        FileUpload fuSlide = (FileUpload)(grvResSlide.Rows[e.RowIndex].FindControl("fuSlide"));
        string filename=Path.GetFileName(fuSlide.FileName);
        try {
            fuSlide.SaveAs(Server.MapPath("Images/ResSlide/"+filename));
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_UpdateResSlide";
            Dal.ParameterCollection = new string[2] { "@SlideID", "@DuongDan" };
            Dal.ValueCollection = new string[2] { i, "Images/ResSlide/" + filename };
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAllResSlide();
        }
        catch { }
    }
    protected void GetAllResSlide()
    {
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllResSlide";
            DataTable db = Dal.GetDataTable();
            grvResSlide.DataSource = db;
            grvResSlide.DataBind();
        }
        catch { }
    }
    protected void GetAllRes()
    {
        try 
        {
            SQLHelper Dal = new SQLHelper { };
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllRes";
            DataTable db = Dal.GetDataTable();
            ddlRes.DataSource = db;
            ddlRes.DataTextField = db.Columns["TenNhaHang"].ToString();
            ddlRes.DataValueField = db.Columns["NhaHangID"].ToString();
            ddlRes.DataBind();
        }
        catch { }
    }
    protected void GetResSlideByResID()
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetResSlideByResID";
            Dal.ParameterCollection = new string[1] { "@NhaHangID" };
            Dal.ValueCollection = new string[1] { ddlRes.SelectedValue };
            DataTable db = Dal.GetDataTable();
            grvResSlide.DataSource = db;
            grvResSlide.DataBind();
        }
        catch { }
    }
    protected void ddlRes_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetResSlideByResID();
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        try {
            string filename = Path.GetFileName(fuSlide.FileName);
            fuSlide.SaveAs(Server.MapPath("~/Images/ResSlide/" + filename));
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_InsertResSlide";
            Dal.ParameterCollection = new string[2] { "@NhahangID", "@DuongDan" };
            Dal.ValueCollection = new string[2] { ddlRes.SelectedValue, "Images/ResSlide/" + filename };
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
        }
        catch { }
    }
}
